package org.blogsite.greencode.struts2.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.blogsite.greencode.bean.InstructorManage_Instructor;
import org.blogsite.greencode.bean.TeachingSupport_Course;
import org.blogsite.greencode.service.TeachingSupport_CourseService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author：jianzhao
 * @date：2010-8-1 下午07:44:29
 */
public class CourseManageAction extends ActionSupport {
	
	private TeachingSupport_Course course;
	private TeachingSupport_CourseService courseService;
	
	private String searchRadio;
	private String keyWord;
	
	HttpServletRequest request;
	
	
	//显示录入课程信息
	@SkipValidation
	public String displaySave() throws Exception {
		try {
			request=ServletActionContext.getRequest();
			request.setAttribute("add", "1");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "SUCCESS_DISPLAY";
	}
	// 显示课程信息查询
	@SkipValidation
	public String displaySearch() throws Exception {

		request = ServletActionContext.getRequest();
		request.setAttribute("search", "1");
		request.setAttribute("searchtitle", "1");
		return "SUCCESS_DISPLAY";
	}
	// 显示修改课程信息
	@SkipValidation
	public String displayModify() throws Exception {
		request = ServletActionContext.getRequest();
		request.setAttribute("cno", request.getParameter("cno"));
		request.setAttribute("name", request.getParameter("name"));
		request.setAttribute("credit", request.getParameter("credit"));
		request.setAttribute("period", request.getParameter("period"));
		request.setAttribute("type", request.getParameter("type"));
		request.setAttribute("describe", request.getParameter("describe"));
		request.setAttribute("modify", "1");
		return "SUCCESS_DISPLAY";
	}
	
	//教学课程-保存
	@SkipValidation
	public String save() throws Exception {
		System.out.println("display save");
		request=ServletActionContext.getRequest();
		if("".equals(course.getName())||course.getName()==null){
			
			request.setAttribute("add", "1");
			request.setAttribute("failuresave_coursenamenull", "1");
			return "FAILURE_SAVE";
		}
		boolean check=courseService.searchCourseByName(course.getName());
		if(check){
			request.setAttribute("add", "1");
			request.setAttribute("failuresave_coursenameexist", "1");
			return "FAILURE_SAVE";
		}else
		courseService.save(course);
		/*
		 * 调用服务层的InstructorService,该对象由spring容器注入
		 */
		request.setAttribute("add", "1");
		request.setAttribute("savesuccess", "1");
		return "SUCCESS_SAVE";
	}
	
	//教学课程-查询
	@SkipValidation
	public String search() throws Exception {
		List list;
		HttpServletRequest hs = ServletActionContext.getRequest();
		if (getSearchRadio().equals("all")||"".equals(getSearchRadio())||getSearchRadio()==null) {
			// 调用服务层,获取所有课程信息的集合
			list = courseService.allSearch();
			hs.setAttribute("allCourse", list);
			if (list.iterator().hasNext() == false) {
				hs.setAttribute("searchnodata", "1");
			} else
				hs.setAttribute("searchhavedata", "1");

		} else if (getSearchRadio().equals("no")) {
			// 调用服务层,获取指定课程号所有课程信息
			try {
				System.out.println(getKeyWord()+"hello");
				list = courseService.byIdSearch(Integer.parseInt(getKeyWord()));
				if (list.iterator().hasNext() == false) {
					hs.setAttribute("searchnodata", "1");
				} else
					hs.setAttribute("searchhavedata", "1");
				hs.setAttribute("allCourse", list);
			} catch (Exception e) {
				System.out.println("format error .");
				hs.setAttribute("searchnodata", "1");
			}
		} else if (getSearchRadio().equals("name")) {
			// 调用服务层,获取指定课程的姓名
			try {
				list =  courseService.byNameSearch(getKeyWord());
				hs.setAttribute("allCourse", list);
				if (list.iterator().hasNext() == false) {
					hs.setAttribute("searchnodata", "1");
				} else
					hs.setAttribute("searchhavedata", "1");
				
			} catch (Exception e) {
				e.printStackTrace();
				hs.setAttribute("searchnodata", "1");
			}
		} else if (getSearchRadio().equals("blear")) {
			// 调用服务层,获取模糊查询所有课程的集合
			list = courseService.byBlearSearch(getKeyWord());
			hs.setAttribute("allCourse", list);
			if (list.iterator().hasNext() == false) {
				System.out.println("no data blear");
				hs.setAttribute("searchnodata", "1");
			} else
				System.out.println("have data blear");
				hs.setAttribute("searchhavedata", "1");
		}
		hs.setAttribute("search", "1");
		return "SUCCESS_DISPLAY";
	}
	
	//教学课程-修改
	public String modify() throws Exception{
//		 TODO Auto-generated method stub
		request=ServletActionContext.getRequest();
		/*
		 * 调用服务层的InstructorService,该对象由spring容器注入
		 */
		courseService.modify(course);
		request.setAttribute("modifysuccess", "1");
		return "SUCCESS_MODIFY";
	}
	
	//教学课程-删除
	@SkipValidation 
	public String delete() throws Exception{
		request=ServletActionContext.getRequest();
		courseService.delete(Integer.parseInt(request.getParameter("id")));
		request.setAttribute("deletesuccess", "1");
		return "SUCCESS_DELETE";
	}
	/*显示删除课程页面*/
	@SkipValidation
	public String displayDelete() throws Exception {
		request = ServletActionContext.getRequest();
		request.setAttribute("search", "1");
		request.setAttribute("delete","1");
		return "SUCCESS_DISPLAY";
	}
	/*修改之前,先查询*/
	@SkipValidation
	public String beforeModify() throws Exception {
		request = ServletActionContext.getRequest();
		request.setAttribute("search", "1");
		request.setAttribute("beforemodify","1");
		return "SUCCESS_DISPLAY";
	}

	public TeachingSupport_Course getCourse() {
		return course;
	}

	public void setCourse(TeachingSupport_Course course) {
		this.course = course;
	}

	public TeachingSupport_CourseService getCourseService() {
		return courseService;
	}

	public void setCourseService(TeachingSupport_CourseService courseService) {
		this.courseService = courseService;
	}

	public String getSearchRadio() {
		return searchRadio;
	}

	public void setSearchRadio(String searchRadio) {
		this.searchRadio = searchRadio;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}
	
}
